Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

228
Visualizações
React | error "Objects are not valid as a React child" when trying to push to state array

I'm learning some React with a project I dreamed up that has data being passed via props and also pubsub messages. I'm passing an email address via pubsub and it comes in fine on the pubsub subscribe method, like this:

myEmailChannel.subscribe("email", (msg) => {
            let data = msg.data;
            
            console.log(data);
                         
        });

The above results in a console log of

value: 'test@test.com'

test@test.com being a fake test email that I sent via pubsub. The problem happens when I try to add the data to an already initialized state array of "emails", I get some crazy error about "Objects are not valid as a React child (found: object with keys {value}). If you meant to render a collection of children, use an array instead." I am trying something like:

 myEmailChannel.subscribe("email", (msg) => {
            let data = msg.data;
            
            console.log(data);
            let emails = [...this.state.emails];
            emails.push({ value: data });
            this.setState({emails});
            
        });

only to get the aforementioned error. Actually, it happens no matter how I try to add the data to the array. I think I understand that I'm just formatting the incoming data incorrectly for the state array? Any help would be much appreciated.

EDIT: I followed the error a little further and it says the error occurs when it tries to iterate over the array for display:

{this.state.emails.map((email, index) => {
                return (

                    <li key={index}>{email}</li>

                );
            })}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As you mentioned, email is an object. When you do something like this

this.state.emails.map((email, index) => (
  <li key={index}>{email}</li>
))

React gets confused. It doesn't know whether it should treat the email object as a string (which is properly what you are trying to do), or render it as a component.

You can do something like this instead.

this.state.emails.map((email, index) => (
  <li key={index}>{email.value}</li>
))
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda